/**
 * @fileoverview Giant Interective Group, Inc. Javascript Library v#version.
 * 该Javascript UI库是基于jQuery的扩展。
 * <pre>
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * </pre>
 * @version #version, #date
 * @author  Zhangkai
 * Depend on jQuery 1.3.x
 *            giant.core.js
 */
if (typeof giant === 'undefined')
    var giant = window.giant = {};
if (typeof giant.ui === 'undefined')
    giant.ui = {};
(function($) {
    giant.ui.select = function(options) {
        this.opts = $.extend({}, giant.ui.select.defaults, options);
        this.$select = $("#" + this.opts.id);
        this._init();
    };
    giant.ui.select.prototype = {
        _init:function() {
            this.$select.width(this.opts.width);
            this.$result = this.$select.find("h4");
            this._eventBind();
            this._selectBind();
        },
        _eventBind:function() {
            var $this = this;
            this.$select.find(".wrap").bind("click", function() {
                $this.$select.find("ul").toggle();
                $this.$select.find(".wrap").removeClass("active").toggleClass("open");
            });
            this.$select.hover(function() {
                  $(this).find(".wrap").addClass("active");
            },function(){
                 $(this).find(".wrap").removeClass("active");
            });
            $(document).bind("click", function(event) {
                if($(event.target).parents("#" + $this.id).size()==0){
                setTimeout(function() {
                    if ($(event.target).parents("#" + $this.opts.id).size() == 0) {
                      $this.$select.find(".wrap").removeClass("active").removeClass("open");
                      $this.$select.find("ul").hide();
                    }
                },100);
                }else{
                    return false;
                }

            });
        },
        _selectBind:function() {
            var $this = this;
            $this.$select.find("ul li").unbind("click").bind("click", function() {
                $this.setValue($(this).attr("name"), $(this).find("a").html());
            });
        },
        setValue:function(value, /*optional*/text) {
            this.$select.find(".wrap").removeClass("open").addClass("select");
            var $li = this.$select.find("ul li[name='" + value + "']");
            text = text || $li.find("a").html();
            this.$select.find("ul").hide();
            if ($li.attr("name")==this.$result.attr("")) {
                return;
            }
            this.$result.attr("name", value);
            this.$result.html(text);
            if ($.isFunction(this.opts.onchange)) {
                this.opts.onchange.call(this, value, text);
            }
        },
        getValue:function() {
            return this.$result.attr("name");
        },
        getText:function() {
            return this.$result.text();
        },
        addItem:function(o, callback) {
            if (!o) {
                return;
            }
            var i;
            var buf = new StringBuilder();
            if (giant.isArray(o)) {
                for (i in o) {
                    if (giant.isString(o[i])) {
                        buf.append('<li name="').append(o[i]).append('"><a href="javascript:void(0)">');
                        buf.append(o[i]).append('</a></li>');
                    } else if (o[i].text && !o[i].text.isEmpty() && o[i].value) {
                        buf.append('<li name="').append(o[i].value).append('"><a href="javascript:void(0)">');
                        buf.append(o[i].text).append('</a></li>');
                    }
                }
            } else if (o.text && !o.text.isEmpty() && o.value) {
                buf.append('<li name="').append(o.value).append('"><a href="javascript:void(0)">');
                buf.append(o.text).append('</a></li>');
            } else if (giant.isString(o)) {
                buf.append('<li name="').append(o).append('"><a href="javascript:void(0)">');
                buf.append(o).append('</a></li>');
            }
            this.$select.find("ul").append(buf.toString());
            this._selectBind();
            if (giant.isFunction(callback))
                callback.call(this);

        },
        addEvent: function(eName, eFunction) {
            this.$select.bind(eName, eFunction);
        },
        /**
         * select的事件。
         *
         * @param {String} eName 要移除的事件。
         */
        removeEvent: function(eName) {
            this.$select.unbind(eName);
        },
        removeItem:function(value) {
            value = value || 0;
            this.$select.find("ul li[name='" + value + "']").remove();
            var $li = this.$select.find("ul li:first");
            if (this.$result.attr("name") == value && $li.size() > 0) {
                this.setValue($li.attr("name"), $li.text());
            }
        },
        removeAllItem:function() {
            this.$select.find("ul").html("");
            this.setValue("", "");
        }
    };

    giant.ui.select.defaults = {
        id:"ui-select",
        width:60,
        data:"",
        onchange:null
    };
})(jQuery);